package org.so.android.imgcopy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.channels.FileChannel;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

public class ImageCopy {

	public void copyFiles(File srcFolder, File trgFolder) {
		System.out.println("working directory: " + srcFolder);
		if(srcFolder.isDirectory()) {
			for(File file : srcFolder.listFiles()) {
				if(file.isFile()) {
					String fileName = file.getName();
					if(fileName.endsWith(".png")) {
						String prefix = fileName.substring(0, fileName.indexOf("."));
						
						Resolution resolution = Resolution.M;
						String newName = "";
						if(prefix.endsWith(Resolution.L.suffix)) {
							resolution = Resolution.L;
							newName = prefix.substring(0, prefix.indexOf(resolution.suffix));
						} else
						if(prefix.endsWith(Resolution.M.suffix)) {
							resolution = Resolution.M;
							newName = prefix.substring(0, prefix.indexOf(resolution.suffix));
						} else
						if(prefix.endsWith(Resolution.H.suffix)) {
							resolution = Resolution.H;
							newName = prefix.substring(0, prefix.indexOf(resolution.suffix));
						} else
						if(prefix.endsWith(Resolution.XH.suffix)) {
							resolution = Resolution.XH;
							newName = prefix.substring(0, prefix.indexOf(resolution.suffix));
						}
						
						try {
							copyFile(file, new File(trgFolder, "\\"+resolution.folder+"\\"+newName+".png"));
							System.out.println("copy: " + fileName + " -> " + trgFolder+"\\"+resolution.folder+"\\"+newName+".png");
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
	}
	
	public void copyFile(File sourceFile, File destFile) throws IOException {
	    if(!destFile.exists()) {
	        destFile.createNewFile();
	    }

	    FileChannel source = null;
	    FileChannel destination = null;

	    try {
	        source = new FileInputStream(sourceFile).getChannel();
	        destination = new FileOutputStream(destFile).getChannel();
	        destination.transferFrom(source, 0, source.size());
	    }
	    finally {
	        if(source != null) {
	            source.close();
	        }
	        if(destination != null) {
	            destination.close();
	        }
	    }
	}

	public static void main( String[] args ) throws URISyntaxException, IOException, ParseException {
		
		CommandLineParser parser = new PosixParser();
		Options options = new Options();
		options.addOption("h", "help", false, "shows the help text");
		options.addOption("s", "src", true, "source folder of the images");
		options.addOption("t", "trg", true, "target folder of the images");
		CommandLine cmd = parser.parse(options, args);
		
		String srcFolder = System.getProperty("user.dir");
		String trgFolder = srcFolder;
		
		if(cmd.hasOption('h')) {
			HelpFormatter lvFormater = new HelpFormatter();
			lvFormater.printHelp("ImageCopy", options);
            return;
		}
		if(cmd.hasOption('s')) {
			srcFolder = cmd.getOptionValue("src");
		}
		if(cmd.hasOption('t')) {
			trgFolder = cmd.getOptionValue("trg");
		}
		
    	ImageCopy ic = new ImageCopy();
		ic.copyFiles(new File(srcFolder), new File(trgFolder));
    }
}
